package com.servlet;

import com.dao.CategoryDao;
import com.dao.impl.CategoryDaoImpl;
import com.entry.Goods;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(urlPatterns = "/Pformation/category.do")
public class CategoryServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        String ainto = req.getParameter("ainto");
        CategoryDao categoryDao = new CategoryDaoImpl();
        int pageSize = req.getParameter("pageSize") == null ? 5 : Integer.parseInt(req.getParameter("pageSize").toString());
        int pageNo = req.getParameter("pageNo") == null ? 1 : Integer.parseInt(req.getParameter("pageNo").toString());
        if(ainto == null){
            PageModel<Goods> pm  = categoryDao.findPc(pageSize,pageNo);
            pm.setPageNav(req.getRequestURI());
            session.setAttribute("ac",pm);
            resp.sendRedirect("./pcategory.jsp");
        }else if("add".equals(ainto)) {
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(req.getParameter("c"));//构造JsonObject对象
            String id = jt.get("id").getAsString();
            String name = jt.get("name").getAsString();
            if("".equals(id) || id == null || "".equals(name) || name == null){
                session.setAttribute("CarpMess", "请完善信息");
            }else {
                if (categoryDao.finId(id) != null) {
                    session.setAttribute("CarpMess", "编号已经存在");
                } else {
                    Goods goods = new Goods();
                    goods.setGid(id);
                    goods.setGname(name);
                    int ro = categoryDao.add(goods);
                    if (ro != 0) {
                        session.setAttribute("CarpMess", "添加成功");
                        PageModel<Goods> pm  = categoryDao.findPc(pageSize,pageNo);
                        pm.setPageNav(req.getRequestURI());
                        session.setAttribute("ac",pm);
                    } else {
                        session.setAttribute("CarpMess", "添加失败");
                    }
                }
            }
            resp.sendRedirect("./pcategory.jsp");
        }else if("update1".equals(ainto)){
            String id = req.getParameter("id");
            Goods goods = categoryDao.finId(id);
            session.setAttribute("goods",goods);
            resp.sendRedirect("./pcategory.jsp");
        }else if("update".equals(ainto)){
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(req.getParameter("c"));//构造JsonObject对象
            String id = jt.get("id").getAsString();
            String name = jt.get("name").getAsString();
            if("".equals(id) || id == null || "".equals(name) || name == null) {
                session.setAttribute("CarUpMess", "请完善信息");
            }else{
                    Goods goods = new Goods();
                    goods.setGid(id);
                    goods.setGname(name);
                    if (categoryDao.Update(goods) != 0) {
                        session.setAttribute("CarUpMess", "修改成功");
                        PageModel<Goods> pm = categoryDao.findPc(pageSize, pageNo);
                        pm.setPageNav(req.getRequestURI());
                        session.setAttribute("ac", pm);
                        session.setAttribute("goods", goods);
                    } else {
                        session.setAttribute("CarUpMess", "修改失败");
                    }
            }
            resp.sendRedirect("./pcategory.jsp");
        }else if("delete".equals(ainto)){
            String id = req.getParameter("id");
            if(categoryDao.delete(id) != 0){
                session.setAttribute("CarDpMess","删除成功");
            }else{
                session.setAttribute("CarDpMess","删除失败");
            }
            PageModel<Goods> pm  = categoryDao.findPc(pageSize,pageNo);
            pm.setPageNav(req.getRequestURI());
            session.setAttribute("ac",pm);
            resp.sendRedirect("./pcategory.jsp");
        }
    }
}
